#coding=utf-8
from datetime import *

class Buy(object):
    goods = [
        {'name':'电脑','price':1999},
        {'name':'鼠标','price':20},
        {'name':'游艇','price':20000},
        {'name':'小米手机','price':1999},
        {'name':'汽车','price':150000},
    ]

    def __init__(self,money, consumption, shopping_cart):
        self.money = money
        self.consumption = consumption
        self.shopping_cart = shopping_cart

    def gouwu(self):   #购物模块
        print("当前余额为:{left_money}".format(left_money=self.money))
        num = int(input("请输入商品序列号:"))
        num -=1
        if self.goods[num]['name'] in self.shopping_cart.keys():
            self.shopping_cart[self.goods[num]['name']]['n'] +=1
        else:
            self.shopping_cart[self.goods[num]['num']] = {'price':self.goods[num]['price'],'n':1}
        self.money -= self.shopping_cart[self.goods[num]['name']]*self.shopping_cart[self.goods[num]['name']]['n']
        self.consumption += self.shooping_cart[self.goods[num]['name']]['price']*self.shopping_cart[self.goods[num]['name']]['n']

    def yichu(self):  #移除购物车中物品
        c = int(input('请输入0/1选择是否移除购物车商品,移除请输入1:'))
        if c==1:
            e = int(input('请输入需要移除的商品序列号: '))
            d = self.goods[e-1]
            if d in self.shopping_cart.keys():
                self.shopping_cart.remove(d)
                self.money = self.money+ self.goods[self.goods.index(d)]['price']
                self.consumption = self.consumption - self.goods[self.goods.index(d)]['price']
            else:
                print('商品不存在')

    def chongzhi(self): #充值模块
        pay = int(input('请输入充值金额'))
        self.money = self.money+pay
        print('您的当前余额为:{money}'.format(money=self.money))

    def main(self):
        print('商品清单:')
        for m,n in enumerate(self.goods,1):
            print(m)
            for v in n.values():
                print(v)
            print('===============================')
        self.consumption =0
        buy = True
        while buy:
            price = 0
            b =1
            if self.money >= price:
                while self.money >= price:
                    self.gouwu()
                    if self.money >= 0:
                        print('您的当前余额为:%d'%self.money)
                        b = int(input('请输入0/1选择是否继续购物,购物请输入1:'))
                        if b == 0:
                            break
                if b == 0:
                    break
            else:
                while self.money < price:
                    a = int(input('您的余额不足,请输入0/1选择是否充值,充值请输入1:'))
                    if a == 1:
                        self.chongzhi()
                    else:
                        break
                    if a == 0:
                        break
        print('您的消费清单为:')
        for m,n in self.shopping_cart.items():
            print(m,n['price'],n['n'])
            print('========================')
        print('您当前余额为:{money},您的消费总额为:{consumption}'.format(money=self.money,consumption=self.consumption))
        return self.consumption


class Atm:
    credit=15000  #信用卡额度
    def __init__(self,balance,debt,remaining_debt,interest,saving,id):
        self.id=id             #信用卡id
        self.balance=balance     #信用卡可用金额
        self.debt=debt           #总欠款
        self.remaining_debt=remaining_debt #剩余欠款
        self.interest=interest      #手续费
        self.saving=saving     #存款
        self.now_time=time.strftime("%Y-%m-%d %H:%M:%S")
        self.now_data=time.strftime("%Y-%m")
        self.struct_time=time.gmtime(time.time())
        if self.struct_time.tm_mday>22:
            self.now_data=self.struct_time.tm_year+'-'+str(int(self.struct_time.tm_mon)+1)

    def account_info(self):#打印账户信息
       return '账户id%s 信用卡额度%s;信用卡可用金额%s;剩余欠款%s;'%(self.id,self.credit,self.balance,self.remaining_debt,)

    def ret_account_info(self):
        return [self.id,self.credit,self.balance,self.debt,self.remaining_debt,self.interest]

    def repay(self,amount):#还款
        self.handel_money('repay',amount)

    def withdraw(self,amount): #取现
        self.handel_money('withdraw',amount)

    def transfer(self,amount): #转账
        self.handel_money('transfer',amount)

    def consume(self,amount):  #消费
        self.handel_money('consume',amount)

    def saves(self,amount):
        self.handel_money('saving',amount)

    def transaction(self,a,amount):
        dic={
            '1':self.repay,
            '2':self.withdraw,
            '3':self.transfer,
            '4':self.consume,
            '5':self.saves
        }
        print("debug: a:",type(a),"amount:",type(amount))

        print(a)
        print(dic[a])
        print(dic["5"])
        dic[a](amount)
        print("end debug")

    def handel_money(self,transaction,amount): #交易类型，
        amount=int(amount)
        interest=amount*settings.TRANSACTION[transaction]['interest'] #手续费计算
        if settings.TRANSACTION[transaction]['action']=='plus':

            if amount<=self.remaining_debt:
                self.remaining_debt-=amount
                self.balance+=amount
            else:
                self.balance+=self.remaining_debt
                self.remaining_debt=0
                self.saving+=amount-self.remaining_debt
        else:

            if self.saving<amount:
                self.saving=0
                a=amount-self.saving
                self.balance-=a+interest-self.saving
                # self.debt+=amount+interest
                self.remaining_debt+=a+interest
        a='time:%s id:%s transaction: %s amount:%s interest %s \n'%(self.now_time,self.id,transaction,amount,interest)
        print(a)
        mulu=os.path.join(settings.ALL_USERS,self.id)
        path_name_liushui=os.path.join(mulu,str(self.id)+'name_liushui',str(self.now_data))

        with open(path_name_liushui,'a')as f:          #记录流水信息
            f.write(a)

        s=[self.balance,self.debt,self.remaining_debt,self.interest,self.saving,]     #更新基本信息
        path_name_base=os.path.join(mulu,str(self.id)+'name_base')
        pickle.dump(s,open(path_name_base,'wb'))


